package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Simple;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.HintDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<Simple>  list;


    public SelectAdapter(Context context){
        mContext = context;
        list = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Simple scanResult = list.get(position);
        Glide.with(mContext).load(scanResult.getImg_url()).into(holder.item_sl_icon);
        holder.item_sl_name.setText(scanResult.getName());
        holder.item_sl_location.setText(scanResult.getSpecific_address());
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(list.get(position),position);
        });
    }

    @Override
    public int getItemCount() {
        return list == null? 0 : list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView item_sl_icon;
        TextView item_sl_location,item_sl_name;

        public ViewHolder(View itemView) {
            super(itemView);
            item_sl_icon = (ImageView) itemView.findViewById(R.id.item_sl_icon);
            item_sl_name = (TextView) itemView.findViewById(R.id.item_sl_name);
            item_sl_location = (TextView) itemView.findViewById(R.id.item_sl_location);
        }
    }



    public void addData(List<Simple> scan){
        if (list != null) {
            list.clear();
            list.addAll(scan);
        }
        notifyDataSetChanged();
    }

    public void addLastData(List<Simple> scan){
        list.addAll(scan);
        notifyDataSetChanged();
    }

    public interface OnItemClickListenter {
        void itemClickListenter(Simple scan, int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

}
